<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class content extends Admin_Controller {

	//--------------------------------------------------------------------

	public function __construct() 
	{
		parent::__construct();

		$this->auth->restrict('User_Details.Content.View');
		$this->load->model('user_details_model', null, true);
		$this->lang->load('user_details');
		
		
	}
	
	//--------------------------------------------------------------------

	/*
		Method: index()
		
		Displays a list of form data.
	*/
	public function index() 
	{
		Assets::add_js($this->load->view('content/js', null, true), 'inline');
		
		Template::set('records', $this->user_details_model->find_all());
		Template::set('toolbar_title', "Manage User Details");
		Template::render();
	}
	
	//--------------------------------------------------------------------

	/*
		Method: create()
		
		Creates a User Details object.
	*/
	public function create() 
	{
		$this->auth->restrict('User_Details.Content.Create');

		if ($this->input->post('submit'))
		{
			if ($insert_id = $this->save_user_details())
			{
				// Log the activity
				$this->activity_model->log_activity($this->auth->user_id(), lang('user_details_act_create_record').': ' . $insert_id . ' : ' . $this->input->ip_address(), 'user_details');
					
				Template::set_message(lang("user_details_create_success"), 'success');
				Template::redirect(SITE_AREA .'/content/user_details');
			}
			else 
			{
				Template::set_message(lang('user_details_create_failure') . $this->user_details_model->error, 'error');
			}
		}
	
		Template::set('toolbar_title', lang('user_details_create_new_button'));
		Template::set('toolbar_title', lang('user_details_create') . ' User Details');
		Template::render();
	}
	
	//--------------------------------------------------------------------

	/*
		Method: edit()
		
		Allows editing of User Details data.
	*/
	public function edit() 
	{
		$this->auth->restrict('User_Details.Content.Edit');

		$id = (int)$this->uri->segment(5);
		
		if (empty($id))
		{
			Template::set_message(lang('user_details_invalid_id'), 'error');
			redirect(SITE_AREA .'/content/user_details');
		}
	
		if ($this->input->post('submit'))
		{
			if ($this->save_user_details('update', $id))
			{
				// Log the activity
				$this->activity_model->log_activity($this->auth->user_id(), lang('user_details_act_edit_record').': ' . $id . ' : ' . $this->input->ip_address(), 'user_details');
					
				Template::set_message(lang('user_details_edit_success'), 'success');
			}
			else 
			{
				Template::set_message(lang('user_details_edit_failure') . $this->user_details_model->error, 'error');
			}
		}
		
		Template::set('user_details', $this->user_details_model->find($id));
	
		Template::set('toolbar_title', lang('user_details_edit_heading'));
		Template::set('toolbar_title', lang('user_details_edit') . ' User Details');
		Template::render();		
	}
	
	//--------------------------------------------------------------------

	/*
		Method: delete()
		
		Allows deleting of User Details data.
	*/
	public function delete() 
	{	
		$this->auth->restrict('User_Details.Content.Delete');

		$id = $this->uri->segment(5);
	
		if (!empty($id))
		{	
			if ($this->user_details_model->delete($id))
			{
				// Log the activity
				$this->activity_model->log_activity($this->auth->user_id(), lang('user_details_act_delete_record').': ' . $id . ' : ' . $this->input->ip_address(), 'user_details');
					
				Template::set_message(lang('user_details_delete_success'), 'success');
			} else
			{
				Template::set_message(lang('user_details_delete_failure') . $this->user_details_model->error, 'error');
			}
		}
		
		redirect(SITE_AREA .'/content/user_details');
	}
	
	//--------------------------------------------------------------------

	//--------------------------------------------------------------------
	// !PRIVATE METHODS
	//--------------------------------------------------------------------
	
	/*
		Method: save_user_details()
		
		Does the actual validation and saving of form data.
		
		Parameters:
			$type	- Either "insert" or "update"
			$id		- The ID of the record to update. Not needed for inserts.
		
		Returns:
			An INT id for successful inserts. If updating, returns TRUE on success.
			Otherwise, returns FALSE.
	*/
	private function save_user_details($type='insert', $id=0) 
	{	
					
		$this->form_validation->set_rules('student_id','Student Id','max_length[3]');			
		$this->form_validation->set_rules('achievepoints_time','Achievement Points -Time-');			
		$this->form_validation->set_rules('achievepoints_class','Achievement Points -Class-');			
		$this->form_validation->set_rules('achievepoints_wiki','Achievement Points -Wiki-');			
		$this->form_validation->set_rules('achievepoints_forum','Achievement Points -Forum-');			
		$this->form_validation->set_rules('achievepoints_main','Achievement Points -Main-');			
		$this->form_validation->set_rules('achievepoints_side','Achievement Points -Side-');			
		$this->form_validation->set_rules('achievepoints_meta','Achievement Points -Meta-');			
		$this->form_validation->set_rules('student_photo','Student Photo','max_length[50]');			
		$this->form_validation->set_rules('student_header','Student Header','max_length[75]');			
		$this->form_validation->set_rules('student_css','Student Css','max_length[35]');			
		$this->form_validation->set_rules('student_assetpath','Student Assetpath','max_length[150]');			
		$this->form_validation->set_rules('student_ownpath','Student Ownpath','max_length[175]');			
		$this->form_validation->set_rules('current_css','Current Css','max_length[175]');

		if ($this->form_validation->run() === FALSE)
		{
			return FALSE;
		}
		
		// make sure we only pass in the fields we want
		
		$data = array();
		$data['student_id']        = $this->input->post('student_id');
		$data['achievepoints_time']        = $this->input->post('achievepoints_time');
		$data['achievepoints_class']        = $this->input->post('achievepoints_class');
		$data['achievepoints_wiki']        = $this->input->post('achievepoints_wiki');
		$data['achievepoints_forum']        = $this->input->post('achievepoints_forum');
		$data['achievepoints_main']        = $this->input->post('achievepoints_main');
		$data['achievepoints_side']        = $this->input->post('achievepoints_side');
		$data['achievepoints_meta']        = $this->input->post('achievepoints_meta');
		$data['student_photo']        = $this->input->post('student_photo');
		$data['student_header']        = $this->input->post('student_header');
		$data['student_css']        = $this->input->post('student_css');
		$data['student_assetpath']        = $this->input->post('student_assetpath');
		$data['student_ownpath']        = $this->input->post('student_ownpath');
		$data['current_css']        = $this->input->post('current_css');
		
		if ($type == 'insert')
		{
			$id = $this->user_details_model->insert($data);
			
			if (is_numeric($id))
			{
				$return = $id;
			} else
			{
				$return = FALSE;
			}
		}
		else if ($type == 'update')
		{
			$return = $this->user_details_model->update($id, $data);
		}
		
		return $return;
	}

	//--------------------------------------------------------------------



}